﻿using Microsoft.Phone.Controls;
using SmarterCharger.App.ViewModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;

namespace SmarterCharger.App.Views
{
    public partial class FirstTimeUserPage : PhoneApplicationPage
    {
        private FirstTimeUserViewModel ViewModel 
        {
            get { return DataContext as FirstTimeUserViewModel; }
            set { DataContext = value; }
        }

        public FirstTimeUserPage()
        {
            InitializeComponent();
            
            ViewModel = ContainerInitializer.Get<FirstTimeUserViewModel>();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            AnimateScreen();
            ViewModel.LoadData();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            ViewModel.SaveData();
        }

        private void AnimateScreen()
        {
            var slideRight = new SlideTransition { Mode = SlideTransitionMode.SlideRightFadeIn };
            var purposePanelTransition = slideRight.GetTransition(PurposePanel);
            purposePanelTransition.Completed += delegate { purposePanelTransition.Stop(); };
            purposePanelTransition.Begin();

            var slideLeft = new SlideTransition { Mode = SlideTransitionMode.SlideLeftFadeIn };
            var startPanelTransition = slideLeft.GetTransition(StartPanel);
            startPanelTransition.Completed += delegate { startPanelTransition.Stop(); };
            startPanelTransition.Begin();

            var slideUp = new SlideTransition { Mode = SlideTransitionMode.SlideUpFadeIn };
            var credentialPanelTransition = slideUp.GetTransition(CredentialsPanel);
            credentialPanelTransition.Completed += delegate { credentialPanelTransition.Stop(); };
            credentialPanelTransition.Begin();

            var choosePanelTransition = slideRight.GetTransition(ChoosePanel);
            choosePanelTransition.Completed += delegate { choosePanelTransition.Stop(); };
            choosePanelTransition.Begin();

            var smartPlugPanelTransition = slideUp.GetTransition(SmartPlugPanel);
            smartPlugPanelTransition.Completed += delegate { smartPlugPanelTransition.Stop(); };
            smartPlugPanelTransition.Begin();

            var conclusionPlugPanelTransition = slideUp.GetTransition(ConclusionPanel);
            conclusionPlugPanelTransition.Completed += delegate 
            { 
                credentialPanelTransition.Stop();
                var rotate = new RotateTransition { Mode = RotateTransitionMode.In180Clockwise };
                var hypnosisTransition = rotate.GetTransition(Hypnosis);
                hypnosisTransition.Completed += delegate { hypnosisTransition.Stop(); };
                hypnosisTransition.Begin();
            };
            conclusionPlugPanelTransition.Begin();

            var chargeButtonTransition = slideRight.GetTransition(Charge);
            chargeButtonTransition.Completed += delegate { chargeButtonTransition.Stop(); };
            chargeButtonTransition.Begin();
        }

        private void UsernameKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                Password.Focus();
        }

        private void PasswordKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                this.Focus();
        }

        private void ScrollTo(UIElement uiElement)
        {
            var transform = uiElement.TransformToVisual(scroller);
            var position = transform.Transform(new Point(0, 0));
            scroller.ScrollToVerticalOffset(position.Y);
        }
    }
}